<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Prefs 02</title>
</head>
<body>

script.path = <?js= script.path.path; ?><br>
script.root = <?js= script.root.path; ?><br>

<?js
const JSWS_PREF_ROOT			= "extensions.jsws.";
const JSWS_PREF_MODULES_ROOT	= JSWS_PREF_ROOT + "modules.";

var prefSvc = Cc["@mozilla.org/preferences-service;1"]
	.getService(Ci.nsIPrefService);
var prefBranch = prefSvc.getBranch(JSWS_PREF_MODULES_ROOT);
//prefBranch.QueryInterface(Ci.nsIPrefBranch2);
var prefType = new Array();
prefType[prefBranch.PREF_INT] = "Integer";
prefType[prefBranch.PREF_BOOL] = "Boolean";
prefType[prefBranch.PREF_STRING] = "String";

var extManager = Cc["@mozilla.org/extensions/manager;1"]
	.createInstance(Ci.nsIExtensionManager);
var modules = new Array();
var children = prefBranch.getChildList("", {});
for (var i = 0; i < children.length; i++) {
	var child = children[i];
	var parts = child.split('.');
	var module = parts.shift();
	var key = parts.join('.');
	if (!modules[module])
		modules[module] = new Array();
	modules[module][key] = prefBranch.getCharPref(child);
	if (key == "id") {
		modules[module]["extPath"] = extManager.getInstallLocation(modules[module][key]).getItemLocation(modules[module][key]).path;
		response.write("Module: " + module + " : extPath : " + modules[module]["extPath"] + "<br>\n");
	}
	response.write("Module: " + module + " : " + key + " : " + modules[module][key] + "<br>\n");
}
modules["root"] = new Array();
modules["root"]["id"] = "root";
modules["root"]["name"] = "root";
modules["root"]["extPath"] = script.mapPath("/../../").path;
modules["root"]["contentPath"] = "/jsws/htdocs";
modules["root"]["virtualPath"] = "/";
?>

<hr>

<?js
for (var key in modules) {
	response.write(key + "<br>\n");
	response.write("&nbsp;&nbsp;&nbsp;Mapped: " + modules[key]["virtualPath"] + "<br>\n");
	response.write("&nbsp;&nbsp;&nbsp;Content: " + modules[key]["extPath"] + modules[key]["contentPath"] + "<br>\n");
}

?>

<hr>

<?js

function mapVirtual (path) {
	var mappedModule = modules["root"];
	for (var key in modules) {
		if (key == "root")
			continue;
		if (path.indexOf(modules[key]["virtualPath"]) == 0)
			if (modules[key]["virtualPath"].length > mappedModule["virtualPath"].length)
				mappedModule = modules[key];
	}
	response.write("Path: " + path + "<br>\n");
	response.write("Matched virtual: " + mappedModule["virtualPath"] + "<br>\n");
	response.write("Real path: " + mappedModule["extPath"] + mappedModule["contentPath"] + "<br>\n");
	response.write("<br>\n");
}

mapVirtual("/");
mapVirtual("/test.jss");
mapVirtual("/jsws/");
mapVirtual("/jsws/test.jss");
mapVirtual("/module1/");
mapVirtual("/module1/test.jss");

?>

<hr>

/module1/test.jss : <?js= script.mapPath("/module1/test.jss").path; ?><br>
test.jss : <?js= script.mapPath("test.jss").path; ?><br>

</body>
</html>